<style type="text/css">
.layui-table-cell{padding:1px 2px;text-align:center;line-height:19px;}
</style>
<table id="dataTable"></table>
<div class="pop-bottom-bar">
    <input type="hidden" class="field-id" name="id">
    <button type="submit" class="layui-btn layui-btn-primary" style="display:none" lay-submit="">新增采购任务</button>
    <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
</div>
{include file="system@block/layui" /}

<script type="text/html" title="操作按钮模板" id="buttonTpl">
	{{# if(d.used_num > 0){ }}
	{{# }else if(d.order_apply_num > 0){ }}
		{{# if(d.action_state==110){ }}
			已出库
		{{# }else{ }}
			<a href="{:url('cancel')}?material_id={{ d.material_id }}&client_ordernum={:$clientOrdernum}&material_stock_id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-danger hisi-ajax"
			 confirm="您确定要执行该操作吗？" refresh="false"  callback="deleteCallback">取消</a>
		{{# } }}
		<!--<a href="javascript:void(0);" data-id="{{ d.id }}" data-material_id="{{ d.material_id }}" class="layui-btn layui-btn-xs confirm">确定</a>-->
	{{# }else{ }}
		<a href="javascript:void(0);" data-id="{{ d.id }}" stock_type="{:$material_type}" data-material_id="{{ d.material_id }}" class="layui-btn layui-btn-xs layui-btn-normal apply">占用</a>
	{{# } }}
</script>
<script type="text/html" title="占用米数模板" id="applyNumTpl">
	{{# if(d.used_num > 0 || d.order_apply_num > 0){ }}
		{{ d.order_apply_num }}
	{{# }else{ }}
		<input type="text" value="" placeholder="输入米数" autocomplete="off" class="layui-input" id="metre_{{ d.id }}">
	{{# } }}
</script>
<script type="text/html" title="使用米数模板" id="usedNumTpl">
	{{# if(d.used_num > 0){ }}
		{{ d.used_num }}
	{{# }else if(d.apply_num > 0){ }}
		<input type="text" value="" placeholder="输入米数" autocomplete="off" class="layui-input" id="true_metre_{{ d.id }}">
	{{# } }}
</script>
<script type="text/html" id="createTimeTpl">
    <div><span title="{{layui.util.toDateString(d.create_time * 1000,'yyyy-MM-dd HH:mm') }}">{{layui.util.toDateString(d.create_time * 1000,'MM-dd') }}</span></div>	
</script>

<script type="text/javascript">
	var factoryKV = {:json_encode($factoryKV)};
	var used_stock_id=0;
    layui.use(['table', 'jquery', 'layer','tool'], function() {
        var $ = layui.jquery;
        var layer = layui.layer;
    	var table = layui.table;
        
        table.render({
            elem: '#dataTable'
            ,url: '{:url()}?list=1&client_ordernum={:$clientOrdernum}&material_id={:$materialId}&material_type={:$material_type}' //数据接口
            ,page: false //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 20
            ,text: {
                none : '暂无相关数据'
            }
            ,cols: [[ //表头
                {field: 'create_time', title: '入库时间',templet: '#createTimeTpl'}
				,{field: 'factory_id', title: '物料属性',templet:function(d){return d.stock_type_name}}
				,{field: 'factory_id', title: '物料名称',templet:function(d){
					return layui.tool.formatMaterialType(d.minfo.type, '', 2) +'<span class="copy-item">'+d.minfo.name+"</span><br>+SKU: <span class='copy-item'>"+d.minfo.material_sku+'</span>'
				}, width:166}
				,{field: 'self_sku', title: '内部sku',templet:function(d){return '<span class="copy-item">'+d.minfo.self_sku+'</span>';}}
				,{field: 'cloth_num', title: '物料id'}
                ,{field: 'factory_id', title: '来源工厂',templet:function(d){return factoryKV[d.factory_id]}}
                ,{field: 'client_ordernum', title: '来源订单号'}
				,{field: 'caigou_number', title: '采购批号'}
                ,{field: 'stock', title: '剩余米数'}
				,{field: 'storehouse_name', title: '货仓/货架',templet:function(d){return d.storehouse_name+"<br>"+d.storeshelf_name;}}
				//,{field: 'apply_num', title: '总占用米数',width:80}
				,{field: 'allstock', title: '整条占用',templet:function(d){return '<a href="javascript:void(0);" data-id='+d.id+' stock='+d.stock+' class="layui-btn layui-btn-xs applyall">整条</a>';}}
                ,{field: 'order_apply_num', title: '占用米数',templet: '#applyNumTpl',width:80}
                //,{field: 'used_num', title: '实际使用',templet: '#usedNumTpl',width:90}
                ,{title: '状态/操作', templet: '#buttonTpl', fixed: 'right'}
            ]]
        });
        
     	// 确定
		$(document).on('click','.confirm',function(){
			var materialStockId = $(this).attr('data-id');
			var materialId = $(this).attr('data-material_id');
			var trueMetre = $('#true_metre_'+materialStockId).val();
			var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
			
			if (reg.test(trueMetre)) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('confirm')}",
		 		   data: "material_stock_id="+materialStockId+"&material_id="+materialId+"&client_ordernum={:$clientOrdernum}&true_metre="+trueMetre,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1,end:function(){table.reload('dataTable');}});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			} else {layer.msg('米数填写错误', {icon: 2,anim: 6})}
		});
		window.deleteCallback = function(obj, res) {
			if (res.code == 1){
				layer.msg(res.msg,{icon: 1,time:200,end:function(){ table.reload('dataTable');}});
			}else{
				layer.msg(res.msg,{icon: 2});
			}
		}
		// 占用
		$(document).on('click','.applyall',function(){
			var materialStockId = $(this).attr('data-id');
			var stock = $(this).attr('stock');
			$("#metre_"+materialStockId).val(stock);
		});
     	
		// 占用
		$(document).on('click','.apply',function(){
			var materialStockId = $(this).attr('data-id');
			var materialId = $(this).attr('data-material_id');
			var metre = $('#metre_'+materialStockId).val();
			var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
			if(used_stock_id==materialStockId){
				//alert('请勿多次点击');
				return;
			}
			used_stock_id=materialStockId;
			if (reg.test(metre)) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('apply')}",
		 		   data: "material_stock_id="+materialStockId+"&material_id="+materialId+"&client_ordernum={:$clientOrdernum}&metre="+metre+"&stock_type={:$material_type}",
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 0) {
						 layer.msg(res.msg, {icon: 1,time:200,end:function(){table.reload('dataTable');}});
					  } else {
					  	 used_stock_id=0;
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			} else {
				used_stock_id=0;
				layer.msg('米数填写错误', {icon: 2,time:200,anim: 6})
			}
		});
     	
    });
</script>